<?

$menuCoreHdr = "<?

session_start();

include_once(\"../classCol.php\");

include_once(\"../helpers/CHtml.php\");
include_once(\"../helpers/CJavascript.php\");
include_once(\"../helpers/Composer.php\");
include_once(\"../helpers/ComposerSourceDb.php\");

include_once(\"models/fetchers.php\");
include_once(\"models/handlers.php\");
include_once(\"controllers/local.php\");

include_once(\"../models/GHandlers.php\");
include_once(\"../models/GFetchers.php\");
include_once(\"../models/GlobalHandlers.php\");
include_once(\"../models/GlobalFetchers.php\");
include_once(\"../controllers/GlobalProcessors.php\");

/**
* Optional defines
*
*/
define(\"_USER_ID\"       , 0);
define(\"_USER_COMPANY\"  , 1);

\$stlButton = \" \";

//check what authoration
\$UID	=	unserialize(\$_SESSION[\"serUID\"]);

\$thisMdl	    = \"".$modulName."\";
\$modulNumber = \"".$menuNumber."\";

\$srcDbHdr = \"USERID_HDR\";
\$srcDbDtl = \"USERID_DTL\";

\$CFetchers  = new CGFetchers;
\$CHandlers  = new CGHandlers;

\$CLHandlers = new CLHandlers;
\$CLFetchers = new CLFetchers;
\$CLocal     = new CLocal;

\$Misc       = new Misc;
\$GHtml      = new CGHtml;
\$GJs        = new CGJavascript;
\$GProcessors= new GlobalProcessors;

\$SourceDb   = new Composer;
\$SourceDb   = \$SourceDb->init(_COMPOSER_TYPE_DB);

\$SourceDb->add(\"srcDbHdr\",      \"".$menuBodySrcDbHdr."\");
\$SourceDb->add(\"srcDbDtl\",      \"".$menuBodySrcDbDtl."\");
\$SourceDb->add(\"srcDbHdrLog\",   \"LOG_".$menuBodySrcDbHdr."\");
\$SourceDb->add(\"srcDbSequence\", \"MST_SEQUENCE\");

\$check_me_authed          = \"NO_OK\";
\$_SESSION[\"modify_mode\"]  = \"N\";

\$_SESSION[\"reauthed\"] = (\$_SESSION[\"reauthed\"] != \"\")? \$_SESSION[\"reauthed\"] : \"N\";
\$_SESSION[\"recheck\"]  = (\$_SESSION[\"recheck\"]  != \"\")? \$_SESSION[\"recheck\"]  : \"N\";

echo  \$GJs->includeThis(_TYPE_CSS,\"/ecs/myMenu.css\");

/**
* Only by pass entry mode and back
* else needs to relogin
*
*/
if(\$_POST[\"entry\"] ||
   \$_POST[\"back\"]  ||
   \$_POST[\"view\"]){

  if(\$_POST[\"entry\"]){

    jumpTo(0,\"entry.php\");
  }else
  if(\$_POST[\"back\"]){

    jumpTo(0,\"../\".\$_SESSION[\"source_caller\"]);
  }else
  if(\$_POST[\"view\"]){

    require(\"viewdboredit_view.php\");
  }else{

    /* Do nothing */
  }
}else{

  if(\$_SESSION[\"reauthed\"]  == \"N\" &&
     \$_SESSION[\"recheck\"]   == \"N\"){

    \$_SESSION[\"temp_radioview\"] = \$_POST[\"radioview\"];
    \$_SESSION[\"temp_selbox\"]    = \$_POST[\"selbox\"];

    \$_SESSION[\"recheck\"] = \"Y\";

		\$flsMessage = \"Validity Check\";
		\$tgtUrl     = \"viewdboredit.php\";
		\$btnLabel   = \"Submit\";

		\$Misc->thisButton(\$_POST,
		                  \$btnName,
		                  \$btnValue);

		\$GProcessors->procChkValidation(\$flsMessage,
                                    \$tgtUrl,
                                    \$btnLabel,
                                    \$btnName,
                                    \$btnValue,
                                    \$stlButton);
  }else
  if(\$_SESSION[\"reauthed\"]  == \"N\" &&
     \$_SESSION[\"recheck\"]   == \"Y\"){

    \$Db = new ".$dbTech.";

  	\$Db->init(\$_SESSION[\"ORCLU\"],
              \$_SESSION[\"OPASS\"],
              \$_SESSION[\"SID\"],
              _NO_COMMENT);

    if(\$Db->connect()){

      \$Misc->fetchRelogin(\$Db,
        			            \$_POST[\"reauth_user\"],
        			            \$_POST[\"reauth_pass\"],
        			            \$_SESSION[\"company\"],
        			            \$srcDbHdr,
        			            \$nrows,
        			            \$_SESSION[\"user_id_reauth\"]);

      if(\$nrows > 0 ){

  			\$mode = \$Misc->thisMode(\$_POST);

  			if(\$UID->getPrivsByNumber( \$modulNumber, \$mode) > 0){

  				\$_SESSION[\"reauthed\"] = \"Y\";

  				\$flsMessage = \"Login Ok\";
      		\$tgtUrl     = \"viewdboredit.php\";
      		\$btnLabel   = \"Continue\";

      		\$Misc->thisButton(\$_POST,
      		                  \$btnName,
      		                  \$btnValue);

  				\$GProcessors->formOkValidated(\$flsMessage,
                				                \$tgtUrl,
                				                \$btnLabel,
               				                  \$btnName,
               				                  \$btnValue,
              				                  \$stlButton);

  			}else{

  				unset(\$_SESSION[\"reauthed\"]);
          unset(\$_SESSION[\"recheck\"]);

  				\$Misc->usrError(\"Authed Error reason : \".\$chk_res.\" \");
  			}
  		}else{

  			\$Db->dc();

  			\$_SESSION[\"recheck\"] = \"Y\";
  			\$flsMessage = \"Validity Check\";
    		\$tgtUrl     = \"viewdboredit.php\";
    		\$btnLabel   = \"Submit\";

    		\$Misc->thisButton(\$_POST,
    		                  \$btnName,
    		                  \$btnValue);

    		\$GProcessors->procChkValidation(\$flsMessage,
                                        \$tgtUrl,
                                        \$btnLabel,
                                        \$btnName,
                                        \$btnValue,
                                        \$stlButton);
  		}
    }else{

      \$Misc->usrDieJs(_DB_CONN_ERROR);
    }
  }else
  if(\$_SESSION[\"reauthed\"]  == \"Y\" &&
     \$_SESSION[\"recheck\"]   == \"Y\"){

    unset(\$_SESSION[\"reauthed\"]);
    unset(\$_SESSION[\"recheck\"]);

    \$check_me_authed = \"OK\";

    if(\$_POST[\"modify\"]){

      if(\$_POST[\"radioview\"] == ''){

  			\$_POST[\"radioview\"] = \$_SESSION[\"temp_radioview\"] ;
  		}

		  require(\"viewdboredit_modify.php\");
    }else
    if(\$_POST[\"delete\"]){

      \$_POST[\"selbox\"] = \$_SESSION[\"temp_selbox\"] ;

      require(\"viewdboredit_delete.php\");
    }else
    if(\$_POST[\"approve\"]){

      \$_POST[\"selbox\"] = \$_SESSION[\"temp_selbox\"] ;

      require(\"viewdboredit_approve.php\");
    }else
    if(\$_POST[\"cancel\"]){

      \$_POST[\"selbox\"] = \$_SESSION[\"temp_selbox\"] ;

      require(\"viewdboredit_cancel.php\");
    }else{

      /* DO nothing */
    }
  }else{

    \$_SESSION[\"recheck\"] = \"Y\";

		\$flsMessage = \"Validity Check\";
		\$tgtUrl     = \"viewdboredit.php\";
		\$btnLabel   = \"Submit\";

		\$Misc->thisButton(\$_POST,
		                  \$btnName,
		                  \$btnValue);

		\$GProcessors->procChkValidation(\$flsMessage,
                                    \$tgtUrl,
                                    \$btnLabel,
                                    \$btnName,
                                    \$btnValue,
                                    \$stlButton);
  }
}
?>  ";

$menuCoreView = "<?

\$Db = new ".$dbTech.";
\$Db->init(\$_SESSION[\"ORCL\"],
           \$_SESSION[\"OPASS\"],
           \$_SESSION[\"SID\"],
           _NO_COMMENT);

if (\$Db->connect()) {

	\$thisMdl     = \"".$modulName."\";
	\$modulNumber = \"".$menuNumber."\";

	\$optPrivs = \"\";

  if(\$UID->getPrivsByNumber( \$modulNumber, _VIEW_RIGHTS) > 0){

  	\$optPrivs .=	\"  <td> \".\$GHtml->formInputButton(\"<font size='5'>View</font>\", // The label
    							                                  \"view\",                       // var Name
    							                                  \"View\",                       // var value
    							                                  \$entryBoxStyle).\" </td>\\n\";   // var properties
  }
  if(\$UID->getPrivsByNumber( \$modulNumber, _MODIFY_RIGHTS) > 0){

  	\$optPrivs .=	\"  <td> \".\$GHtml->formInputButton(\"<font size='5'>Modify</font>\", // The label
            							                          \"modify\",                       // var Name
            							                          \"Modify\",                       // var value
            							                          \$entryBoxStyle).\" </td>\\n\";     // var properties
  }
  if(\$UID->getPrivsByNumber( \$modulNumber, _APPROVED_RIGHTS) > 0){

  	\$optPrivs	.=	\"  <td> \".\$GHtml->formInputButton(\"<font size='5'>Approve</font>\", // The label
            							                          \"approve\",                       // var Name
            							                          \"Approve\",                       // var value
            							                          \$entryBoxStyle).\" </td>\\n\";      // var properties
  }
  if(\$UID->getPrivsByNumber( \$modulNumber, _DELETE_RIGHTS) > 0){

  	\$optPrivs	.=	\"  <td> \".\$GHtml->formInputButton(\"<font size='5'>Cancel</font>\", // The label
            							                          \"cancel\",                       // var Name
            							                          \"Cancel\",                       // var value
            							                          \$entryBoxStyle).\" </td>\\n\";     // var properties
  }
  if(\$UID->getPrivsByNumber( \$modulNumber, _EDIT_RIGHTS) > 0){

  	\$optPrivs	.=	\"  <td> \".\$GHtml->formInputButton(\"<font size='5'>Delete</font>\", // The label
            							                          \"delete\",                       // var Name
            							                          \"Delete\",                       // var value
            							                          \$entryBoxStyle).\" </td>\\n\";     // var properties
  }

  \$jobId = \$_POST[\"radioview\"];

	if(\$_POST[\"transVariable\"] != true){

  	echo  \$GJs->includeThis(_TYPE_CSS,\"/ecs/myMenu.css\").
          \$GJs->includeThis(_TYPE_JS ,\"/ecs/javascript/GMisc.js\").
          \$GJs->includeThis(_TYPE_JS ,\"/ecs/javascript/GAjax.js\").
          \$GJs->includeThis(_TYPE_JS ,\"../javascript/misc.js\").
          \$GJs->includeThis(_TYPE_JS ,\"../javascript/ajax.js\").
          \$GJs->stopRKey().

  	      \"<form action=viewdboredit.php name='crtList' method=post>\\n\".
  				\"<table border='1'>\\n\".
  				\"<tr>\\n\".
  				\"<input type='submit' value='back' name='back'>\\n\".
  			  \$optPrivs.
  			  \"<input type='hidden' name='radioview' value='\".\$_POST[\"radioview\"].\"'>\\n\".
  			  \"<input type='hidden' name='selbox[]'  value='\".\$_POST[\"radioview\"].\"'>\\n\".
  				\"</tr>\\n\".
  				\"</table>\\n\".
  				\"</form>\\n\";

  }else{

    /* Do nothing */
  }

  require(\"views/view_html.php\");
}else{

	\$Misc->usrDieJs(_DB_CONN_ERROR);
}
?> ";

$menuCoreModify = "<?

\$check_me_authed = \"NO_OK\";

if((\$jid = \$_POST[\"radioview\"]) || (\$jid = \$_POST[\"job_id_posted\"]) ){

	\$Db = new ".$dbTech.";
  \$Db->init(\$_SESSION[\"ORCL\"],
             \$_SESSION[\"OPASS\"],
             \$_SESSION[\"SID\"],
             _NO_COMMENT);

	if (\$Db->connect()) {

  	\$srcDbHdr = \"".$srcDbHdr."\";
    \$srcDbDtl = \"".$srcDbDtl."\";

		\$_SESSION[\"jid\"]  = \$_POST[\"radioview\"];
		\$jobId            = &\$_SESSION[\"jid\"];

		/**
		* Fetch global header
		* And check the status if it is valid
		* where STATUS = _NOT_ACTIVE
		*
		*/
		\$CFetchers->fetchLstDataHeader(\$Db,
                                   \$jobId,
                                   \$srcDbHdr,
                                   \$nHeader,
                                   \$lstHeader);

		if(\$lstHeader[\"STATUS\"][0] == _NOT_ACTIVE ||
		   \$lstHeader[\"STATUS\"][0] == _CANCELED){

  		/**
  		* Init the header vars
  		*
  		*/
  		\$_POST[\"trDateEnt\"]     = date(\"d-m-Y\",
  		                               strtotime(\$lstHeader[\"TR_DATE\"][0]));

  		/**
  		* Init the detail
  		*
  		*/
  		\$CFetchers->fetchLstDataDetail(\$Db,
                                     \$jobId,
                                     \$srcDbDtl,
                                     \$nDetail,
                                     \$lstDetail);

  		for(\$iRow = 0; \$iRow < \$nDetail; \$iRow++){


  		}

  		/**
  		* Check if this tx isn't canceled
  		* YES: it is modify
  		* NO : Add new entry with this copy
  		*
  		*/
  		if(\$lstHeader[\"STATUS\"][0] != _CANCELED){

  		  \$_SESSION[\"modify_mode\"]= \"Y\";
  		}else{

  		  \$_SESSION[\"modify_mode\"]= \"N\";
  		}

  		/**
  		* This is only to by pass
  		* the entry_detail precheck
  		*
  		*/
  		//\$_POST[\"entry\"]         = \"byPass\";
  		\$_SESSION[\"init_num\"]   = \$nDetail;

  		\$Db->dc();

  		require(\"entry_restore_me.php\");

  	}else{

  	  \$Misc->usrDieJs(\"Tx can't be modified, it has been approved\");
  	}
	}else{

	  \$Misc->usrDieJs(_DB_CONN_ERROR);
	}
}else{

	\$Misc->usrDieJs(\"Error:Cant retrieved job_id\");
}

?> ";

$menuCoreApprove = "<?

if(\$check_me_authed == \"OK\"){

	\$today_date			= date(\"d-m-Y\");
  \$refIdTag        = \"".$menuNumber."\";
	\$srcDbHdr        = \"".$srcDbHdr."\";
	\$srcDbDtl        = \"".$srcDbDtl."\";
	\$srcDbHdrLog     = \"LOG_".$srcDbHdr."\";
	\$srcDbSequence   = \"MST_SEQUENCE\";

	if(\$_POST[\"selbox\"] != ''){

		\$UID     = unserialize(\$_SESSION[\"serUID\"]);
    \$userId  = \$UID->getUID();

    \$Db = new ".$dbTech.";
    \$Db->init(\$_SESSION[\"ORCL\"],
               \$_SESSION[\"OPASS\"],
               \$_SESSION[\"SID\"],
               _NO_COMMENT);

    if(\$Db->connect()){

			if(sizeof(\$_POST[\"selbox\"]) == 0){

				\$Misc->usrDieJs(\"Error: Nothing to approve\");
			}else{

				foreach(\$_POST[\"selbox\"] as \$apprThisJobId){

					/**
      		* Fetch global header
      		* And check the status if it is valid
      		* where STATUS = _NOT_ACTIVE
      		*
      		*/
      		\$CFetchers->fetchLstDataHeader(\$Db,
                                         \$apprThisJobId,
                                         \$srcDbHdr,
                                         \$nHeader,
                                         \$lstHeader);

				  if(\$lstHeader[\"STATUS\"][0] == _NOT_ACTIVE){

			      /**
			      * Build up the REFERENCE number
			      *
			      */
			      \$thisPeriode    = &\$_SESSION[\"sysPeriode\"];

			      \$seqEditStatus  = false;

            \$thisSequence   = \"".$refSequence."\";

          	while(\$seqEditStatus == false){

            	\$CFetchers->fetchCurSequence(\$Db,
                          	               \$thisSequence,
                          	               \$thisPeriode,
                          	               \$srcDbSequence,
                        	                 \$srcJidIndex);

            	\$CHandlers->initCurSequence(\$Db,
                          	              \$thisSequence,
                          	              \$srcJidIndex,
                          	              \$thisPeriode,
                          	              \$srcDbSequence,
                          	              \$seqEditStatus);
            }

            /**
            * Extract the system period
            *
            */
            \$tmpDate = explode(\"-\", \$thisPeriode);
            \$wrkDate = \$tmpDate[0]. /* This period month */
                       \"-\".
                       \$tmpDate[1]; /* This period year */

            \$refId  = addDigit((string) \$srcJidIndex).
                      \$srcJidIndex.
                      \"/\" .
                      \$refIdTag.
                      \"/\" .
                      \$wrkDate;

            \$CHandlers->procApproval(\$Db,
                                     \$apprThisJobId,
                                     \$refId,
                                     \$userId,
                                     \$srcDbHdr);

            /**
            * Extra work is to input A/P and
            * check the serial number of
            * each tx and do input into inventory
            *
            */
            \$jobId     = \$apprThisJobId;
            \$isApprove = true;

            /**
            * Fetch the source date
            * since it is so significant
            *
            */
            fetchTxHeader(\$Db,
                          \$thisJobId,
                          \$srcDbHdr,
                          \$resHeader);

            \$srcDate    = date(\"d-m-Y\",
                               strtotime(\$resHeader[\"TR_DATE\"][0]));

            /**
            * Insert Log
            *
            */
            \$CHandlers->insToDbLog(\$Db,
                                   \$today_date,
                                   \$apprThisJobId,
                                   \$userId,
                                   \$_SERVER[\"REMOTE_ADDR\"],
                                   _APPROVE_MODE,
                                   \$srcDbHdrLog);

				  }else{

				    \$Misc->usrDieJs(\"Error: Tx is ACTIVE/APPROVED/CANCELED\");
				  }

				}

				//\$Db->commit();

				if(\$Db->getStatus() == true){

      	  \$Misc->usrMsg(\"Approval Complete\");
      	}else{

      	  \$Misc->usrMsg(\"Approval Process Interupted\");
      	}

      	\$Db->dc();

			}
		}else{

			\$Misc->usrDieJs(_DB_CONN_ERROR);
		}
	}else{

		\$Misc->usrDieJs(\"Error: Unknown selection or bad ID retrieved\");
		//jumpTo(2,\"../\".\$_SESSION[\"source_caller\"]);
	}
}else{

	\$Misc->usrDieJs(\"NOT AUTHED\");
}
?> ";

$menuCoreCancel = "<?

if(\$_POST[\"selbox\"] != ''){

	//reauth the user, if \$_SESSION[\"reauthed\"] != Y bail out
	if(\$check_me_authed == \"OK\"){

		\$today_date 			= date(\"d-m-Y\");

  	\$srcDbHdr			     = \"".$srcDbHdr."\";
    \$srcDbHdrLog        = \"LOG_".$srcDbHdr."\";
    \$srcDbDtl           = \"".$srcDbDtl."\";
    \$srcDbSequence      = \"MST_SEQUENCE\";

    \$UID     = unserialize(\$_SESSION[\"serUID\"]);
    \$userId  = \$UID->getUID();

    \$Db = new ".$dbTech.";
    \$Db->init(\$_SESSION[\"ORCL\"],
               \$_SESSION[\"OPASS\"],
               \$_SESSION[\"SID\"],
               _NO_COMMENT);

    if(\$Db->connect()){

			if(sizeof(\$_POST[\"selbox\"]) == 0){

				\$Misc->usrDieJs(\"Error: nothing to be cancel\");
			}else{

				foreach(\$_POST[\"selbox\"] as \$cclThisJobId){

					/**
      		* Fetch global header
      		* And check the status if it is valid
      		* where STATUS = _NOT_ACTIVE
      		*
      		*/
      		\$CFetchers->fetchLstDataHeader(\$Db,
                                         \$cclThisJobId,
                                         \$srcDbHdr,
                                         \$nHeader,
                                         \$lstHeader);

					if(\$lstHeader[\"STATUS\"][0] != _ACTIVE){

						\$Misc->usrDieJs(\"Error: Tx with JobId: \".\$cclThisJobId.\" is not yet approved.\");
					}else{

						\$CHandlers->procCancel(\$Db,
            						           \$cclThisJobId,
            						           \$userId,
            						           \$srcDbHdr);

						/**
            * Extra work is to update the picked
            * jobId and check if recvQty
            * Greater than 0
            * YES: set status to _OUTSTANDING
            * NO : set status to _ACTIVE
            *
            */
            \$isCancel   = true;
            \$jobId      = \$cclThisJobId;

            /**
            * Insert Log
            *
            */
            \$CHandlers->insToDbLog(\$Db,
                                   \$today_date,
                                   \$cclThisJobId,
                                   \$userId,
                                   \$_SERVER[\"REMOTE_ADDR\"],
                                   _CANCEL_MODE,
                                   \$srcDbHdrLog);
					}

				}

				\$Db->commit();

				if(\$Db->getStatus() == true){

      	  \$Misc->usrMsg(\"Cancel Complete\");
      	}else{

      	  \$Misc->usrMsg(\"Cancel Process Interupted\");
      	}

      	\$Db->dc();

				//GoBack(\"2\",\"../\".\$_SESSION[\"source_caller\"]);
			}
		}else{

			\$Misc->usrDieJs(_DB_CONN_ERROR);
		}
	}else{

		\$Misc->usrDieJs(\"NOT AUTHED\");
	}
}else{

  \$Misc->usrDieJs(\"Error: No Selection\");
}
?> ";

$menuCoreDelete = "<?

if(\$_POST[\"selbox\"] != ''){

	if(\$check_me_authed == \"OK\"){

		\$today_date 			= date(\"d-m-Y\");

		\$srcDbHdr			  = \"".$srcDbHdr."\";
    \$srcDbHdrLog     = \"LOG_".$srcDbHdr."\";
    \$srcDbDtl        = \"".$srcDbDtl."\";

    \$UID     = unserialize(\$_SESSION[\"serUID\"]);
    \$userId  = \$UID->getUID();

		\$Db = new ".$dbTech.";
    \$Db->init(\$_SESSION[\"ORCL\"],
               \$_SESSION[\"OPASS\"],
               \$_SESSION[\"SID\"],
               _NO_COMMENT);

    if (\$Db->connect()) {


			if(sizeof(\$_POST[\"selbox\"]) == 0){

				\$Misc->usrDieJs(\"Error nothing to be delete\");
			}else{

				foreach(\$_POST[\"selbox\"] as \$delThisJobId){

          /**
      		* Fetch global header
      		* And check the status if it is valid
      		* where STATUS = _NOT_ACTIVE
      		*
      		*/
      		\$CFetchers->fetchLstDataHeader(\$Db,
                                         \$delThisJobId,
                                         \$srcDbHdr,
                                         \$nHeader,
                                         \$lstHeader);

          if(\$lstHeader[\"STATUS\"][0] == _NOT_ACTIVE){

            /**
          	* Delete the previous entry
          	*
          	*/
          	\$CHandlers->dbEntDelete(\$Db,
                      	            \$delThisJobId,
                      	            \$srcDbHdr);

            \$CHandlers->dbEntDelete(\$Db,
                      	            \$delThisJobId,
                      	            \$srcDbDtl);

            /**
            * Insert Log
            *
            */
            \$CHandlers->insToDbLog(\$Db,
                                   \$today_date,
                                   \$delThisJobId,
                                   \$userId,
                                   \$_SERVER[\"REMOTE_ADDR\"],
                                   _DELETE_MODE,
                                   \$srcDbHdrLog);

          }else{

            \$Misc->usrDieJs(\"Error: Tx is ACTIVE/APPROVED/CANCELED\");
          }

			  }

  			\$Db->commit();

  			if(\$Db->getStatus() == true){

      	  \$Misc->usrMsg(\"Delete Complete\");
      	}else{

      	  \$Misc->usrMsg(\"Delete Process Interupted\");
      	}

      	\$Db->dc();

  			//jumpTo(2,\"../\".\$_SESSION[\"source_caller\"]);
      }
		}else{

			\$Misc->usrDieJs(_DB_CONN_ERROR);
		}
	}else{

		\$Misc->usrDieJs(\"Error: Connection to oracle Failed\");
	}
}else{

  \$Misc->usrDieJs(\"Error: No Selection\");
}
?> ";

?>